package servlet.user;

/**
 * servlet.user
 * User: mcw
 * Date: 2024/12/20 下午4:31
 * motto:   春天（spring）没有虫子（bug)
 * Description:
 * Version: V1.0
 */

import bean.pojo.User;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.r.R;
import service.UsersService;
import service.impl.UsersServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 个人信息管理
 */
@WebServlet(name = "PersonalInformation", value ="/personalInfo/*")
public class PersonalInformation extends BaseServlet {
    public R getUserInfoByToken(HttpServletRequest request){
        //前端提交的是表单数据
        //axios 默认提交是 json数据
        String  token =request.getParameter("token");//token是json数据 name
        //  =request.getParameter就不能获取这数据因为数据请求流中
        System.out.println(token);
        //返回用户信息给前端 ServletContext
        User user =(User) getServletContext().getAttribute(token);
        System.out.println("userInfo:user:"+user);
        UsersService usersService=new UsersServiceImpl();
        User user1=usersService.findUserById(user.getUserId());
        System.out.println("user1:"+user1);
        //结果集
        R r;
        //判断是否登录成功
        if(user==null){
            r=R.error("没有登录");
        }else{
            r=R.ok(user1);
        }
        return r;
    }

    /**
     * 更改用户信息
     * @param request
     * @return
     */
    public R updatePersonalInfo(HttpServletRequest request) throws IOException {
        BufferedReader reader=request.getReader();
        //数据
        String  data="";
        //接收数据
        data= reader.readLine();
        //把数据格式化(解析)
        User user = JSONObject.parseObject(data, User.class);
        System.out.println("updatePersonalInfo:user:"+user);
        String  token =request.getParameter("token");//token是json数据 name
        //  =request.getParameter就不能获取这数据因为数据请求流中
        System.out.println(token);
        //返回用户信息给前端 ServletContext
        User user1 =(User) getServletContext().getAttribute(token);
        System.out.println("updatePersonalInfo:user1:"+user1);
        UsersService usersService=new UsersServiceImpl();
        Integer update=usersService.updatePersonalInfo(user,user1.getUserId());
        //结果集
        R r;
        //判断是否登录成功
        if(update<1){
            r=R.error("修改失败");
        }else{
            r=R.ok();
        }
        return r;
    }

    /**
     * 查询除了登录用户外是否还有相同的用户名
     * @param request
     * @return
     */
    public R tfExistName(HttpServletRequest request){
        String  token =request.getParameter("token");
        System.out.println("tfExistName:token:"+token);
        String userName = request.getParameter("userName");
        System.out.println("tfExistName:userName:"+userName);
        //返回用户信息给前端 ServletContext
        User user =(User) getServletContext().getAttribute(token);
        System.out.println("tfExistName:userName:"+user);
        UsersService usersService=new UsersServiceImpl();
        Boolean valid=usersService.tfExistName(userName,user.getUserId());
        R  r= R.ok().addData("valid",valid);
        return r;
    }
}
